/**
 * 弹幕React Hooks
 * 提供便捷的弹幕功能钩子
 */

import { useRef, useCallback, useState, useEffect } from 'react'
import type { BarrageAPI, BarrageItem, BarrageConfig, BarrageEvents } from '../types'

export interface UseBarrageOptions {
  config?: BarrageConfig
  events?: BarrageEvents
  enabled?: boolean
}

export interface UseBarrageReturn {
  /** 弹幕API引用 */
  barrageRef: React.RefObject<BarrageAPI>
  /** 添加弹幕 */
  addBarrage: (barrage: Omit<BarrageItem, 'id'>) => string
  /** 移除弹幕 */
  removeBarrage: (id: string) => boolean
  /** 清空弹幕 */
  clearBarrages: () => void
  /** 暂停弹幕 */
  pauseBarrages: () => void
  /** 恢复弹幕 */
  resumeBarrages: () => void
  /** 活跃弹幕数量 */
  activeCount: number
  /** 最大容量 */
  maxCapacity: number
  /** 是否已满 */
  isFull: boolean
}

/**
 * 弹幕功能Hook
 */
export function useBarrage(options: UseBarrageOptions = {}): UseBarrageReturn {
  const barrageRef = useRef<BarrageAPI>(null)
  const [activeCount, setActiveCount] = useState(0)
  const [maxCapacity, setMaxCapacity] = useState(0)

  const addBarrage = useCallback((barrage: Omit<BarrageItem, 'id'>) => {
    if (!barrageRef.current) return ''
    return barrageRef.current.add(barrage)
  }, [])

  const removeBarrage = useCallback((id: string) => {
    if (!barrageRef.current) return false
    return barrageRef.current.remove(id)
  }, [])

  const clearBarrages = useCallback(() => {
    barrageRef.current?.clear()
  }, [])

  const pauseBarrages = useCallback(() => {
    barrageRef.current?.pause()
  }, [])

  const resumeBarrages = useCallback(() => {
    barrageRef.current?.resume()
  }, [])

  // 监听容量变化
  useEffect(() => {
    const originalOnCapacityChange = options.events?.onCapacityChange
    
    if (options.events) {
      options.events.onCapacityChange = (active, max) => {
        setActiveCount(active)
        setMaxCapacity(max)
        originalOnCapacityChange?.(active, max)
      }
    }
  }, [options.events])

  return {
    barrageRef,
    addBarrage,
    removeBarrage,
    clearBarrages,
    pauseBarrages,
    resumeBarrages,
    activeCount,
    maxCapacity,
    isFull: activeCount >= maxCapacity
  }
}

/**
 * 弹幕输入Hook
 */
export function useBarrageInput() {
  const [text, setText] = useState('')
  const [color, setColor] = useState('#FFFFFF')

  const clearInput = useCallback(() => {
    setText('')
  }, [])

  const createBarrage = useCallback((overrides?: Partial<Omit<BarrageItem, 'id'>>) => {
    return {
      text,
      color,
      type: 'scroll' as const,
      ...overrides
    }
  }, [text, color])

  return {
    text,
    setText,
    color,
    setColor,
    clearInput,
    createBarrage
  }
}

/**
 * 弹幕统计Hook
 */
export function useBarrageStats(barrageRef: React.RefObject<BarrageAPI>) {
  const [stats, setStats] = useState({
    activeCount: 0,
    maxCapacity: 0,
    utilization: 0
  })

  useEffect(() => {
    const updateStats = () => {
      if (!barrageRef.current) return

      const activeCount = barrageRef.current.getActiveCount()
      const maxCapacity = barrageRef.current.getMaxCapacity()
      const utilization = maxCapacity > 0 ? (activeCount / maxCapacity) * 100 : 0

      setStats({ activeCount, maxCapacity, utilization })
    }

    const interval = setInterval(updateStats, 1000)
    updateStats() // 立即更新一次

    return () => clearInterval(interval)
  }, [barrageRef])

  return stats
}
